﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class admin_students : System.Web.UI.Page
{
    protected Administrator admin;
    protected int SortID;
    private int DoctorGuid;
    private int ExpertGuid;
    private int Offset;
    protected void Page_Load(object sender, EventArgs e)
    {
        admin = Administrator.Check();
        if (admin == null)
        {
            Session.Add(Params.SystemError, "请先登录");
            Response.Redirect(Params.RootHttp + "default.aspx");
        }
        if (Request.Params["d"] == null)
        {
            this.DoctorGuid = 0;
        }
        else
        {
            this.DoctorGuid = Convert.ToInt32(Server.UrlDecode(Request.Params["d"]));
        }
        if (Request.Params["e"] == null)
        {
            this.ExpertGuid = -1;
        }
        else
        {
            this.ExpertGuid = Convert.ToInt32(Server.UrlDecode(Request.Params["e"]));
        }
        if (Request.Params["o"] == null)
        {
            this.Offset = 0;
        }
        else
        {
            this.Offset = Convert.ToInt32(Server.UrlDecode(Request.Params["Offset"]));
        }
        if (!Page.IsPostBack)
        {
            this.MyDataBind();
        }
    }

    public void MyDataBind()
    {
        this.conNav.Title = "学生列表";
        this.SortID = 1;
        this.BindDoctors();
        this.BindExperts();
        this.BindStudents();
    }

    private void BindDoctors() 
    {
        this.ddDoctors.DataSource = Doctor.Doctors(true);
        this.ddDoctors.DataBind();
        this.ddDoctors.SelectedValue = this.DoctorGuid.ToString();

        this.ddDoctor.DataSource = Doctor.Doctors(false);
        this.ddDoctor.DataBind();
    }

    private void BindExperts()
    {
        this.ddExperts.DataSource = Doctor.Experts(true);
        this.ddExperts.DataBind();
        this.ddExperts.SelectedValue = this.ExpertGuid.ToString();
    }

    private void BindStudents()
    {
        this.dgStudent.CurrentPageIndex = this.Offset / this.dgStudent.PageSize + 1;
        string sql = "select count(*) as n from Student s";
        ArrayList ord = new ArrayList();
        if (this.DoctorGuid != 0)
        {
            ord.Add("s.DoctorGuid="+this.DoctorGuid.ToString());
        }
        if (this.ExpertGuid != -1)
        {
            ord.Add("s.ExpertGuid=" + this.ExpertGuid.ToString());
        }
        sql = sql + Fun.GenerateOrderSQL(ord, true,false);
        int rc = Convert.ToInt32(Fun.GetField(sql));
        this.dgStudent.RecordCount = rc;
        ord.Add("u.UserType='"+LwUser.StudentType+"'");
        string o = Fun.GenerateOrderSQL(ord);
        sql = "select top "+this.dgStudent.PageSize.ToString()+" u.Guid from Users u Join Student s on u.Guid=s.UserGuid where "+o+" and u.Guid not in (select top "+
            this.Offset.ToString()+" u.Guid from Users u Join Student s on u.Guid=s.UserGuid where "+o+" order by u.UserID asc ) order by u.UserID asc";
        DataTable dt = Fun.CreateDt(sql);
        this.repStudents.DataSource = dt;
        this.repStudents.DataBind();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string uid = this.tbID.Text.Trim();
        string n = this.tbName.Text.Trim();
        string email = this.tbEmail.Text.Trim();
        int dg = Convert.ToInt32(this.ddDoctor.SelectedValue);
        if (uid == "" || n == "")
        {
            this.info.SetMessage("姓名和ID不能为空！");
            return;
        }
        string tip = "";
        int g = Student.Add(uid, n, email, dg, ref tip);
        if (g == 0)
        {
            Session.Add(Params.SystemError, tip);
            Response.Redirect(Request.RawUrl);
        }
        Session.Add(Params.SystemError, "添加成功！");
        Response.Redirect(Params.AdminFolderHttp + "students.aspx?guid=" + g.ToString());
    }
    protected void dgStudents_PageChanged(object src, Wuqi.Webdiyer.PageChangedEventArgs e)
    {
        Response.Redirect(Params.AdminFolderHttp + "students.aspx?d=" + Server.UrlEncode(this.DoctorGuid.ToString()) + "&e=" + Server.UrlEncode(this.ExpertGuid.ToString()) + "&o=" + Convert.ToString(this.dgStudent.PageSize * (e.NewPageIndex - 1)));
    }
    protected void ddDoctors_SelectedIndexChanged(object sender, EventArgs e)
    {
        Response.Redirect(Params.AdminFolderHttp + "students.aspx?d=" + Server.UrlEncode(this.ddDoctors.SelectedValue) + "&e=" + Server.UrlEncode(this.ExpertGuid.ToString())+"&o="+this.Offset.ToString());
    }
    protected void ddExperts_SelectedIndexChanged(object sender, EventArgs e)
    {
        Response.Redirect(Params.AdminFolderHttp + "students.aspx?d=" + Server.UrlEncode(this.DoctorGuid.ToString()) + "&e=" + Server.UrlEncode(this.ddExperts.SelectedValue) + "&o=" + this.Offset.ToString());
    }
}
